package cn.edu.answer.controller;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.answer.po.Question;
import cn.edu.answer.service.QuestionService;
import cn.edu.answer.utils.Page;



@Controller
public class QuestionController {
	//依赖注入
		@Autowired
		private QuestionService questionService;
		
		/**
		 *  学校列表
		 */
		@RequestMapping(value = "/question/list.action")
		public String list(@RequestParam(defaultValue="1")Integer page,
				@RequestParam(defaultValue="10")Integer rows, Model model) {
			// 所有学生列表
			Page<Question> question = questionService.findQuestionList(page, rows);
			model.addAttribute("page", question);	
			
			return "question_list";
		}
		
		/**
		 * 创建用户
		 */
		@RequestMapping("/question/createQuestion.action")
		@ResponseBody
		public String createQuestion(Question question,HttpSession session) {
			System.out.println("======"+ question);
		    // 执行Service层中的创建方法，返回的是受影响的行数
		    int rows = questionService.createQuestion(question);
		    if(rows > 0){
		        return "OK";
		    }else{
		        return "FAIL";
		    }
		}
		
		/**
		 * 通过id获取学校信息
		 */
		@RequestMapping("/question/getQuestionById.action")
		@ResponseBody
		public Question getQuestionById(String qid) {
			 System.out.println("====="+qid);
		    
			 Question question=questionService.getQuestionById(qid);
		    System.out.println("====="+qid);
		    return question;
		}
		/**
		 * 更新学校信息
		 */
		@RequestMapping("/question/updateQuestion.action")
		@ResponseBody
		public String questionUpdate(Question question) {
			   System.out.println("======="+question);
		    int rows = questionService.updateQuestion(question);
			   System.out.println("======="+rows);
		    if(rows > 0){
		        return "OK";
		    }else{
		        return "FAIL";
		    }
		}
		
		/**
		 * 删除学校信息
		 */
		@RequestMapping("/question/deleteQuestion.action")
		@ResponseBody
		public String questionDelete(String qid) {
		    int rows = questionService.deleteQuestion(qid);
		    if(rows > 0){			
		        return "OK";
		    }else{
		        return "FAIL";			
		    }
		}
}
